package com.lill.system.serivce.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lill.common.component.transaction.CommonTransaction;
import com.lill.common.component.transaction.TM;
import com.lill.common.component.util.IdUtil;
import com.lill.system.constant.SystemConstant;
import com.lill.system.dao.ISysRoleDao;
import com.lill.system.dao.ISysUserDao;
import com.lill.system.model.SysRoleEntity;
import com.lill.system.model.SysUserEntity;
import com.lill.system.service.ISysRoleService;

/**
 * 角色
 *
 * @author lill
 */
@Service("sysRoleService")
public class SysRoleServiceImpl implements ISysRoleService {
    @Autowired
    private ISysRoleDao sysRoleDao;
    @Autowired
    private SysRoleMenuServiceImpl sysRoleMenuService;
    @Autowired
    private ISysUserDao sysUserDao;
    @Autowired
    private SysRoleDeptServiceImpl sysRoleDeptService;

    public SysRoleEntity queryObject(String roleId) {
        return sysRoleDao.queryObject(roleId);
    }

    public List<SysRoleEntity> queryList(Map<String, Object> map) {
        return sysRoleDao.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return sysRoleDao.queryTotal(map);
    }

    @Transactional
    public int save(SysRoleEntity role,SysUserEntity user) {
        role.setCreateTime(new Date());
        role.setCreateUser(user.getId());
        role.setRoleId(IdUtil.createIdbyUUID());
        int result=sysRoleDao.save(role);

        //检查权限是否越权
        checkPrems(role);

        //保存角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getId(), role.getMenuIdList());

        //保存角色与部门关系
        sysRoleDeptService.saveOrUpdate(role.getId(), role.getDeptIdList());
        return result;
    }

    @Transactional
    public int update(SysRoleEntity role,SysUserEntity user) {
        role.setUpdateTime(new Date());
        role.setUpdateUser(user.getId());
        int result=sysRoleDao.update(role);

        //检查权限是否越权
        checkPrems(role);

        //更新角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getId(), role.getMenuIdList());
        //保存角色与部门关系
        sysRoleDeptService.saveOrUpdate(role.getId(), role.getDeptIdList());
        return result;
    }

    @Transactional
    public int deleteBatch(String[] roleIds) {
        return sysRoleDao.deleteBatch(roleIds);
    }

    public List<String> queryRoleIdList(String createUser) {
        return sysRoleDao.queryRoleIdList(createUser);
    }

    /**
     * 检查权限是否越权
     */
    private void checkPrems(SysRoleEntity role) {
        //如果不是超级管理员，则需要判断角色的权限是否超过自己的权限
        if (SystemConstant.SUPER_ADMIN.equals(role.getCreateUser())) {
            return;
        }

        //查询用户所拥有的菜单列表
        List<String> menuIdList = sysUserDao.queryAllMenuId(role.getCreateUser());

        //判断是否越权
        if (!menuIdList.containsAll(role.getMenuIdList())) {
            throw new RuntimeException("新增角色的权限，已超出你的权限范围");
        }
    }

	@Override
	public int deleteLogicByMap(Map<String, Object> params) {
		return sysRoleDao.deleteLogicByMap(params);
	}

	@CommonTransaction({TM.sys})
	@Override
	public int copyRoles(String[] roleIds, String companyId, SysUserEntity curUser) {
		int result=0;
		if(null!=roleIds&&roleIds.length>0){
			List<SysRoleEntity> roleList=new ArrayList<SysRoleEntity>();
			SysRoleEntity role=null;
			for (String roleId : roleIds) {
				role=queryObject(roleId);
				if(null!=role){
					role.setCompanyId(companyId);
					role.setMenuIdList(sysRoleMenuService.queryMenuIdList(roleId));
					roleList.add(role);
				}
			}
			if(!roleList.isEmpty()){
				for (SysRoleEntity sysRole : roleList) {
					result+=save(sysRole, curUser);
				}
			}
		}
		return result;
	}

}
